clear
use dta/KDB1921_55673.DTA 

gen knr=v1
rename v1 knavn
rename v2 eligiblemen
rename v3 eligiblewomen
rename v4 eligibleoverall
rename v5 eligiblesuspmen
rename v6 eligiblesuspwomen
rename v7 personalvotesmen
rename v8 personalvotewomen
rename v9 personalvoteoverall
rename v10 mailmen
rename v11 mailwomen
rename v12 mailoverall
rename v13 castedvotesoverall
rename v14 rejectedvotesoverall
rename v15 approvedvotesoverall
rename v16 votesDNA
rename v17 votesNSA
rename v18 votesRF
rename v19 votesV
rename v20 votesSP     /* OBS "L" IN ELECTION PDF , WILL CHANGE NAME IN NEXT ELECTION */ 
rename v21 votesH_FV
rename v22 votesOTH

/* COUNTIES */

g cnr = 0
replace cnr =1 if knr<200
replace cnr =2 if knr>200 & knr<300
replace cnr =3 if knr==301
replace cnr =4 if knr>400 & knr<500
replace cnr =5 if knr>500 & knr<600
replace cnr =6 if knr>600 & knr<700
replace cnr =7 if knr>700 & knr<800
replace cnr =8 if knr>800 & knr<900
replace cnr =9 if knr>900 & knr<1000
replace cnr =10 if knr>1000 & knr<1100
replace cnr =11 if knr>1100 & knr<1200
replace cnr =12 if knr>1200 & knr<1300
replace cnr =13 if knr>1300 & knr<1400
replace cnr =14 if knr>1400 & knr<1500
replace cnr =15 if knr>1500 & knr<1600
replace cnr =16 if knr>1600 & knr<1700
replace cnr =17 if knr>1700 & knr<1800
replace cnr =18 if knr>1800 & knr<1900
replace cnr =19 if knr>1900 & knr<2000
replace cnr =20 if knr>2000 

/* VALGKRETS */

g valgkrets = 0
replace valgkrets =1 if knr<200
replace valgkrets =2 if knr>200 & knr<300
replace valgkrets =3 if knr==301
replace valgkrets =4 if knr>400 & knr<500
replace valgkrets =5 if knr>500 & knr<600
replace valgkrets =6 if knr>600 & knr<700
replace valgkrets =7 if knr>700 & knr<800
replace valgkrets =8 if knr>800 & knr<900
replace valgkrets =9 if knr>900 & knr<1000
replace valgkrets =10 if knr>1000 & knr<1100
replace valgkrets =11 if knr>1100 & knr<1200
replace valgkrets =12 if knr>1200 & knr<1300
replace valgkrets =13 if knr>1300 & knr<1400
replace valgkrets =14 if knr>1400 & knr<1500
replace valgkrets =15 if knr>1500 & knr<1600
replace valgkrets =16 if knr>1600 & knr<1700
replace valgkrets =17 if knr>1700 & knr<1800
replace valgkrets =18 if knr>1800 & knr<1900
replace valgkrets =19 if knr>1900 & knr<2000
replace valgkrets =20 if knr>2000 


*** KJOPSTEDER

/* 4 REP */
replace valgkrets=21 if knr==101  /* fredrikshald */  
replace valgkrets=21 if knr==102  /* sarpsborg */
replace valgkrets=21 if knr==103  /* fredrikstad */
replace valgkrets=21 if knr==104  /* moss */
replace valgkrets=21 if knr==203  /* drøbak */
/* 7 REP */
replace valgkrets=22 if knr==301  /* oslo  */  
/* 3 REP */
replace valgkrets=23 if knr==401  /* hamar */   
replace valgkrets=23 if knr==402  /* kongsvinger */
replace valgkrets=23 if knr==501  /* lillehammer */
replace valgkrets=23 if knr==502  /* gjøvik */
/* 3 REP */
replace valgkrets=24 if knr==602 /* drammen */
replace valgkrets=24 if knr==604  /* kongsberg */
replace valgkrets=24 if knr==601  /* hønefoss */
/* 4 REP */
replace valgkrets=25 if knr==703  /* horten */
replace valgkrets=25 if knr==705  /* tønsberg */
replace valgkrets=25 if knr==706  /* sandefjord */
replace valgkrets=25 if knr==707  /* larvik */
replace valgkrets=25 if knr==702  /* holmestrand */
/* 5 REP */
replace valgkrets=26 if knr==807  /* notodden */
replace valgkrets=26 if knr==806  /* skien */
replace valgkrets=26 if knr==805  /* porsgrund */
replace valgkrets=26 if knr==804  /* brevik */
replace valgkrets=26 if knr==801  /* kragerø */
replace valgkrets=26 if knr==901  /* risør */
replace valgkrets=26 if knr==903  /* arendal */
replace valgkrets=26 if knr==904  /* grimstad */
/* 7 REP */
replace valgkrets=27 if knr==1001 /* kristiansand */
replace valgkrets=27 if knr==1002 /* mandal */
replace valgkrets=27 if knr==1004 /* flekkefjord */
replace valgkrets=27 if knr==1103 /* stavanger */
replace valgkrets=27 if knr==1106 /* haugesund */
/* 5 REP */
replace valgkrets=28 if knr==1301 /* bergen */
/* 3 REP */
replace valgkrets=29 if knr==1502 /* molde */
replace valgkrets=29 if knr==1501 /* ålesund */
replace valgkrets=29 if knr==1503 /* kristiansund */
/* 5 REP */
replace valgkrets=30 if knr==1601 /* trondheim */
replace valgkrets=30 if knr==1701 /* levanger */
/* 4 REP */
replace valgkrets=31 if knr==1804 /* bodoe */
replace valgkrets=31 if knr==1805 /* narvik */
replace valgkrets=31 if knr==1902 /* tromsoe */
replace valgkrets=31 if knr==2002 /* vardoe */
replace valgkrets=31 if knr==2003 /* vadsoe */
replace valgkrets=31 if knr==2001 /* hammerfest */



gen rep=0
replace rep=6 if valgkrets==1
replace rep=7 if valgkrets==2
replace rep=7 if valgkrets==4
replace rep=6 if valgkrets==5
replace rep=5 if valgkrets==6
replace rep=4 if valgkrets==7
replace rep=5 if valgkrets==8
replace rep=4 if valgkrets==9
replace rep=4 if valgkrets==10
replace rep=5 if valgkrets==11
replace rep=8 if valgkrets==12
replace rep=5 if valgkrets==14
replace rep=7 if valgkrets==15
replace rep=6 if valgkrets==16
replace rep=5 if valgkrets==17
replace rep=8 if valgkrets==18
replace rep=5 if valgkrets==19
replace rep=3 if valgkrets==20
replace rep=4 if valgkrets==21
replace rep=7 if valgkrets==22
replace rep=3 if valgkrets==23
replace rep=3 if valgkrets==24
replace rep=4 if valgkrets==25
replace rep=5 if valgkrets==26
replace rep=7 if valgkrets==27
replace rep=5 if valgkrets==28
replace rep=3 if valgkrets==29
replace rep=5 if valgkrets==30
replace rep=4 if valgkrets==31


**************** UPDATE OCTOBER 2017 - FIXING ERRORS IN MUNICIPALITY LEVEL DATA  - originally only fixed for the SMD level data ***************************
/* was 6065 (=2869 + 3196 )*/
replace votesH_FV=2304 if knr ==1501  	/* was 2305 (=2304 + 1 )*/
replace votesH_FV=554 if knr ==1502  	/* was 557 (=554 + 3 )*/
replace votesH_FV=11 if knr ==1503  	/* was 3203 (=11 + 3192 )*/
replace votesOTH=1 if knr ==1501 
replace votesOTH=3 if knr ==1502 
replace votesOTH=3192 if knr ==1503 


/* RADIKALE VENSTRE AND VENSTRE RAN INDEPENDENTLY , BUT ARE LUMPED TOGETHER IN ELECTION DATA */
** let "radikale venstre" be RF
replace votesV=116 if knr==801
replace votesV=26 if knr==804
replace votesV=22 if knr==805
replace votesV=740 if knr==806
replace votesV=215 if knr==807
replace votesV=449 if knr==901
replace votesV=98 if knr==903
replace votesV=16 if knr==904

replace votesRF=415 if knr==801
replace votesRF=145 if knr==804
replace votesRF=801 if knr==805
replace votesRF=507 if knr==806
replace votesRF=167 if knr==807
replace votesRF=55 if knr==901
replace votesRF=406 if knr==903
replace votesRF=326 if knr==904

replace votesV=0 if knr==401
replace votesV=0 if knr==402
replace votesV=0 if knr==501
replace votesV=0 if knr==502

replace votesRF=249 if knr==401
replace votesRF=63 if knr==402
replace votesRF=118 if knr==501
replace votesRF=221 if knr==502
**** update 2017 end


/* MUNICIPALITY LEVEL FILE */
gen year=1921
sort knr
save dta/Storting1921_municipality, replace


/* CREATING DATA SET ON THE VALGKRETS LEVEL */

collapse (sum) eligible* casted* approved* votes* (mean) rep, by(valgkrets)

gen year=1921

/* relection was held in valgkrets=7 , see pdf page 78-79 */
*replace castedvotesoverall=26676 if valgkrets==7  /* 29.may, 2016: use "godkjente stemmer" for vestfold since "avgitte stemmer" is not provided, the JoP paper then uses turnout based on the first held election */
*replace approvedvotesoverall=26676 if valgkrets==7
*replace votesDNA=3923 if valgkrets==7
*replace votesNSA=1048 if valgkrets==7
*replace votesRF=0 if valgkrets==7
*replace votesV=4533 if valgkrets==7
*replace votesSP=4081 if valgkrets==7
*replace votesH_FV=13097 if valgkrets==7
*replace votesOTH=0 if valgkrets==7

** what we have is "stemmesedler" , it appears that it is possible to make alterations to the lists, resulting in "listestemmer"
** "listestemmer" is what is used for the allocation of seats 

/* 2017 - THIS IS NO LONGER NECESSARY, GOOD TO SEE THAT THERE ARE NO REPLACEMENTS */
/* RADIKALE VENSTRE AND VENSTRE RAN INDEPENDENTLY , BUT ARE LUMPED TOGETHER IN ELECTION DATA */
replace votesV=1682 if valgkrets==26   /* was 4504 (=1682 + 2822) */
replace votesRF=2822 if valgkrets==26
replace votesV=0 if valgkrets==23   /* was 651  */
replace votesRF=651 if valgkrets==23

/* 2017 - THIS IS NO LONGER NECESSARY, GOOD TO SEE THAT THERE ARE NO REPLACEMENTS */
/* H_FV AND "KRISTIANSUNDS SAMLINGSPARTI" RAN INDEPENDENTLY , BUT ARE LUMPED TOGETHER IN ELECTION DATA */
replace votesH_FV=2869 if valgkrets==29   /* was 6065 (=2869 + 3196 )*/
replace votesOTH=3196 if valgkrets==29

/* CREATING TURNOUT AND VOTE SHARE VARIABLES */

gen turnout=castedvotesoverall/eligibleoverall

/* TOTAL VOTES FOR EACH PARTY DOES NOT SUM EXACTLY TO APPROVEDVOTESOVERALL.. "ville" IS MISSING (19.03.2015)? */
*replace approvedvotesoverall=votesDNA+ votesNSA+ votesRF+ votesV+ votesSP+ votesH_FV+ votesOTH

foreach party in DNA NSA RF V SP H_FV OTH{
gen votesh`party'=votes`party'/approvedvotesoverall
}

sort year valgkrets
save dta/Storting1921, replace
